﻿@typeparam TItem
@inherits BaseAfterRenderComponent

<Modal @ref="modalRef" Closing="@PopupClosing" Closed="@Cancel">
    <ModalContent Size="@PopupSize">
        <Form>
            <ModalHeader>
                <ModalTitle>
                    @if ( TitleTemplate != null && EditItem != null )
                    {
                        @TitleTemplate( new( EditItem, EditState, EditState == DataGridEditState.Edit
                            ? Localizer.Localize( ParentDataGrid.Localizers?.EditLocalizer, "Edit" )
                            : Localizer.Localize( ParentDataGrid.Localizers?.CreateLocalizer, "Create" ) ) )
                    }
                    else
                    {
                        @( EditState == DataGridEditState.Edit
                            ? Localizer.Localize( ParentDataGrid.Localizers?.ModalRowEditLocalizer, "Row Edit" )
                            : Localizer.Localize( ParentDataGrid.Localizers?.ModalRowCreateLocalizer, "Row Create" ))
                    }
                </ModalTitle>
                <CloseButton Clicked="@CloseModal" AutoClose="false" />
            </ModalHeader>
            <ModalBody>
                <Validations @ref="validations" Mode="ValidationMode.Manual" StatusChanged="@ValidationsStatusChanged" Model="@ValidationItem" HandlerType="@ParentDataGrid?.ValidationsHandlerType">
                    @if ( ParentDataGrid.ShowValidationsSummary )
                    {
                        <Field Display="@(ParentDataGrid.HasValidationsSummary || isInvalid ? DisplayAlways : DisplayNone)">
                            <ValidationSummary Label="@ParentDataGrid.ValidationsSummaryLabel" Errors="@ParentDataGrid.ValidationsSummaryErrors" />
                        </Field>
                    }
                    <Fields>
                        @{
#pragma warning disable CS0618 // Type or member is obsolete
                        }
                        @foreach ( var column in OrderedEditableColumns )
                        {
                            <Field ColumnSize="@(column.EditFieldColumnSize ?? column.PopupFieldColumnSize ?? ColumnSizeIsHalfOnDesktop)">
                                <FieldLabel>
                                    @column.Caption
                                </FieldLabel>
                                <FieldBody>
                                    <_DataGridCell TItem="TItem" Column="@column" Item="@EditItem" ValidationItem="@ValidationItem" CellEditContext="@EditItemCellValues[column.ElementId]" ShowValidationFeedback="@ParentDataGrid.ShowValidationFeedback" />
                                </FieldBody>
                            </Field>
                        }
                        @{
#pragma warning restore CS0618 // Type or member is obsolete
                        }
                    </Fields>
                </Validations>
            </ModalBody>
            <ModalFooter>
                @if ( ParentDataGrid.CommandColumn is null || ParentDataGrid.CommandColumn.CancelCommandAllowed )
                {
                    var cancelButtonString = Localizer.Localize( ParentDataGrid.Localizers?.CancelButtonLocalizer, "Cancel" );

                    @if ( ParentDataGrid.CommandColumn?.CancelCommandTemplate != null )
                    {
                        @ParentDataGrid.CommandColumn.CancelCommandTemplate( new()
                        {
                            Clicked = EventCallback.Factory.Create( this, CloseModal ),
                            LocalizationString = cancelButtonString,
                            Item = EditItem,
                        } )
                    }
                    else
                    {
                        <Button Color="Color.Link" Clicked="@CloseModal">
                            @cancelButtonString
                        </Button>
                    }
                }
                @if ( ParentDataGrid.CommandColumn is null || ParentDataGrid.CommandColumn.SaveCommandAllowed )
                {
                    var saveButtonString = Localizer.Localize( ParentDataGrid.Localizers?.SaveButtonLocalizer, "Save" );

                    @if ( ParentDataGrid.CommandColumn?.SaveCommandTemplate != null )
                    {
                        @ParentDataGrid.CommandColumn.SaveCommandTemplate( new()
                        {
                            Clicked = callbackFactory.Create( this, Save ),
                            LocalizationString = saveButtonString,
                            Item = EditItem,
                        } )
                    }
                    else
                    {
                        @if ( ParentDataGrid.SubmitFormOnEnter )
                        {
                            <Button Type="ButtonType.Submit" PreventDefaultOnSubmit Color="Color.Link" Clicked="@Save">
                                @saveButtonString
                            </Button>
                        }
                        else
                        {
                            <Button Color="Color.Link" Clicked="@Save">
                                @saveButtonString
                            </Button>
                        }
                    }
                }
            </ModalFooter>
        </Form>
    </ModalContent>
</Modal>